import axios from "axios"
//在组件外部使用编程式
import { createBrowserHistory } from "history"

let history = createBrowserHistory()

let http = axios.create({
    baseURL: "/api",
    withCredentials: true,
    timeout: 1000 * 10
})

//请求拦截器
http.interceptors.request.use(config => {
    //携带token过去
    let token = JSON.parse(localStorage.getItem('qf-token') as string)
    // let token = localStorage.getItem('qf-token') as string
    config.headers!.authorization = token as string
    return config
})

//响应拦截器
http.interceptors.response.use(config => {
    console.log(config)
    if (config.data.code === "1004") {
        console.log('77777')
        //校验失败了,登入过期
        //跳转到登入首页 清除token
        localStorage.removeItem('qf-token')
        history.push('/login')
        window.location.reload()
    }
    return config
})


export default http